﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fmis21_10.Models;

namespace Fmis21_10.Controllers
{
    public class PlayerHistoryController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /PlayerHistory/

        public ActionResult Index()
        {
            var playerhistories = db.PlayerHistories.Include(p => p.Player);
            return View(playerhistories.ToList());
        }

        //
        // GET: /PlayerHistory/Details/5

        public ActionResult Details(int id = 0)
        {
            PlayerHistory playerhistory = db.PlayerHistories.Find(id);
            if (playerhistory == null)
            {
                return HttpNotFound();
            }
            return View(playerhistory);
        }

        //
        // GET: /PlayerHistory/Create

        public ActionResult Create()
        {
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName");
            return View();
        }

        //
        // POST: /PlayerHistory/Create
        public PartialViewResult MultiCreate()
        {
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "PlayerName");
            ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ClubName");
            return PartialView("MultiCreate");
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PlayerHistory playerhistory, FormCollection f)
        {
            if (ModelState.IsValid)
            {
                var ClubIdArray = f.GetValues("ClubId");
                var playerIdArray = f.GetValues("PlayerId");
                var StartDateArray = f.GetValues("StartDate");
                var EndDateArray = f.GetValues("EndDate");
                var DescriptionArray = f.GetValues("Description");
                var PlayerCodeArray = f.GetValues("PlayerCode");

                for (int i = 0; i < playerIdArray.Count(); i++)
                {
                    playerhistory.ClubId = Convert.ToInt32(ClubIdArray[i]);
                    playerhistory.PlayerId = Convert.ToInt32(playerIdArray[i]);
                    playerhistory.StartDate = Convert.ToDateTime(StartDateArray[i]);
                    playerhistory.EndDate = Convert.ToDateTime(EndDateArray[i]);
                    playerhistory.Description = DescriptionArray[i];
                    playerhistory.PlayerCode = PlayerCodeArray[i];

                    db.PlayerHistories.Add(playerhistory);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }

            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", playerhistory.PlayerId);
            return View(playerhistory);
        }

        //
        // GET: /PlayerHistory/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PlayerHistory playerhistory = db.PlayerHistories.Find(id);
            if (playerhistory == null)
            {
                return HttpNotFound();
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", playerhistory.PlayerId);
            return View(playerhistory);
        }

        //
        // POST: /PlayerHistory/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(PlayerHistory playerhistory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(playerhistory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", playerhistory.PlayerId);
            return View(playerhistory);
        }

        //
        // GET: /PlayerHistory/Delete/5

        public ActionResult Delete(int id = 0)
        {
            PlayerHistory playerhistory = db.PlayerHistories.Find(id);
            if (playerhistory == null)
            {
                return HttpNotFound();
            }
            return View(playerhistory);
        }

        //
        // POST: /PlayerHistory/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PlayerHistory playerhistory = db.PlayerHistories.Find(id);
            db.PlayerHistories.Remove(playerhistory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}